﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace Server.Users
{
    internal class UserHandler
    {
        private static readonly string file = @"Users\users.xml";
        public static bool IsUserLoginValid(string username, string password)
        {
            XElement element = XElement.Load(file);

            var user = from u in element.Descendants("user")
                       where (string)u.Element("username") == username && (string)u.Element("password") == password
                       select new { Username = username, Password = password };

            if (user.Count() > 0)
                return true;
            return false;
        }

        public static bool AddUser(string username, string password)
        {
            XElement element = XElement.Load(file);

            var user = (from u in element.Descendants("user")
                       where (string)u.Element("username") == username
                       select new { Username = username }).SingleOrDefault();

            if (user != null)
                return false;

            element.Add(new XElement("user", new XElement("username", username), new XElement("password", password)));
            element.Save("users.xml");
            
            return true;
        }
    }
}
